/*
 * Copyright (c)  2008 Sebastien Tremblay
 *
 * id3-tag-builder - Id3TagBuilder.java
 *
 * All source and documentation is copyrighted by Sebastien Tremblay
 * (seb at ryders dot net) and made available under the Apache License 2.0.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * $Id: Id3TagBuilder.java 33 2010-04-02 07:47:55Z ryders $
 */
package com.empireone.id3tagbuilder;

import com.empireone.id3tagbuilder.classes.Format;
import com.empireone.id3tagbuilder.classes.Path;
import com.empireone.id3tagbuilder.settings.ApplicationSettings;
import com.empireone.id3tagbuilder.threads.StatusUpdateListener;
import com.empireone.id3tagbuilder.threads.WorkerPool;
import java.util.Vector;

/**
 * 
 * @author Sebastien Tremblay
 */
public class Id3TagBuilder implements StatusUpdateListener {

    private ApplicationSettings settings;
    private StatusUpdateListener listener;
    private WorkerPool workers;
    private int workload, progress;

    /**
     *
     * @param settings
     * @param listener
     */
    public Id3TagBuilder(ApplicationSettings settings, StatusUpdateListener listener) {
        this.settings = settings;
        this.listener = listener;
    }

    /**
     *
     * @return
     */
    public ApplicationSettings getSettings() {
        return settings;
    }

    /**
     *
     * @param format
     * @param paths
     * @throws Id3TagBuilderException
     */
    public void run(Format format, Vector<Path> paths) throws Id3TagBuilderException {
        workload = progress = 0;
        workers = new WorkerPool(this, settings, format, paths);
        workers.start();
    }

    /**
     *
     */
    public void abort() {
        workers.halt();
    }

    /**
     *
     * @return
     */
    public boolean isWorking() {
        return workers != null;
    }

    /**
     *
     * @param value
     */
    @Override
    public void updateWorkload(int value) {
        listener.updateWorkload(value);
        workload += value;
    }

    /**
     *
     * @param value
     */
    @Override
    public void updateProgress(int value) {
        listener.updateProgress(value);
        progress += value;

        if (progress == workload) {
            status(MsgType.COMPLETED);
        }
    }

    /**
     *
     * @param type
     */
    @Override
    public void status(MsgType type) {

        switch (type) {
            case COMPLETED:
                workers.reset();
                workers = null;
                break;
            case HALTED:
                workers = null;
                break;
        }

        listener.status(type);
    }

    /**
     * 
     * @param type
     * @param str
     */
    @Override
    public void status(MsgType type, String str) {

        switch (type) {

            case JOB_CREATED:
                str = "NEW: " + str;
                break;

            case JOB_STARTED:
                str = "START: " + str;
                break;

            case JOB_HALTED:
                str = "HALT: " + str;
                break;

            case JOB_COMPLETED:
                str = "END: " + str;
                break;

            case PROGRESS:
                break;

            case WARNING:
                str = "WARN: " + str;
                break;

            case ERROR:
                str = "ERR: " + str;
                break;

            case HALTED:
                workers = null;
                break;
        }

        listener.status(type, str);
    }
}
